package com.zghhost.stockweb.controller;

import com.zghhost.stockweb.pojo.StockDayInfo;
import com.zghhost.stockweb.service.impl.KLineService;
import com.zghhost.stockweb.service.impl.SinaDadanService;
import com.zghhost.stockweb.util.CalendarUtil;
import com.zghhost.stockweb.util.HttpUtil;
import com.zghhost.stockweb.util.IOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @author
 */
@Controller
public class HelloController{
    private static final String url = "http://quotes.money.163.com/service/chddata.html?code=%s&start=%s&end=%s";

    @Autowired
    SinaDadanService sinaDadanService;

    @Autowired
    KLineService kLineService;

    @RequestMapping(value = "/")
    public String index(){
        return "index";
    }

    /**
     * 沪0开头 深1开头 +6位代码
     */
    @ResponseBody
    @RequestMapping(value = "/data")
    public Object data(@RequestParam String code)throws Exception{
        Date endDate = new Date();
        Date startDate = CalendarUtil.add(endDate, Calendar.YEAR,-5);

        String end = CalendarUtil.format(endDate,CalendarUtil.yyyyMMdd);
        String start = CalendarUtil.format(startDate,CalendarUtil.yyyyMMdd);

        if(code.startsWith("3")){
            code = "1"+code;
        }else{
            code = "0"+code;
        }

        List<StockDayInfo> kline = kLineService.getKLine(code,start,end);

        Object[][] sdata = new Object[kline.size()][6];

        for (int ic=kline.size()-1,i=ic; i >= 0; i--) {
            StockDayInfo k = kline.get(i);
            sdata[ic-i] = new Object[]{k.getRiqi(),k.getKaipan(),k.getShowpan(),k.getZuigao(),k.getZuidi(),k.getChengjiaoliang()};
        }

        return sdata;
    }
}